/**
 * Created by biyajuan on 2017/2/27.
 */
//获取验证码
var countdown=60;
var toast = new auiToast({});

$(function () {
    //滚动数据,成功借款的用户
    var data = {'no_need_sign':true};
    $.ajax({
        type: "POST",
        url: CONFIG.API_URL+"/Home/UserCenter/getDemographic",
        data: data,
        dataType: "jsonp",
        success: function(response){
            var res_data = eval(response);
            if(res_data.code == CONFIG.SUCC_CODE){
                $.each(res_data.data, function(index, item){
                    $('#g_'+index).text('恭喜手机号 '+item['mobile']+' 成功借款'+parseInt(item['money']/100)+'元');
                });
            }
        }
    });

    //注册新用户
    $("#register").click(function () {
        //按钮置灰,防止多次点击
        $(this).removeClass("aui-btn").addClass("maibei-disable-bgcolor");

        var mobile = $("#phone").val().trim();
        var verify_code = $("#verify").val().trim();
        var password = $("#password").val().trim();

        //手机号、验证码、密码校验
        var match_mobile = /^1[345678]\d{9}$/;
        if(!match_mobile.test(mobile)){
            toast.fail({title:"错误手机号", duration:2000});
            $(this).removeClass("maibei-disable-bgcolor").addClass("aui-btn");
            return false;
        }
        if(verify_code.length != 6){
            toast.fail({title:"验证码不正确", duration:2000});
            $(this).removeClass("maibei-disable-bgcolor").addClass("aui-btn");
            return false;
        }
        if(password.length < 6 || password.length > 18){
            toast.fail({title:"密码格式错误", duration:2000});
            $(this).removeClass("maibei-disable-bgcolor").addClass("aui-btn");
            return false;
        }

        //注册新用户
        var md5_pass = hex_md5(password);
        var sha1_pass = hex_sha1(md5_pass);
        var split_pass = sha1_pass.split("").reverse().join("");
        var reg_pass = split_pass.toUpperCase();
        var channel_id = getUrlParam('channelId');
        if(channel_id==null || channel_id.trim().length<=0){
            //alert('参数不正确');
            $(this).removeClass("maibei-disable-bgcolor").addClass("aui-btn");
            //return false;
        }
        var type = ((getUrlParam('type')!=null)&&(getUrlParam('type').trim().length>0))?getUrlParam('type').trim():'unknown';
        var other = ((getUrlParam('other')!=null)&&(getUrlParam('other').trim().length>0))?getUrlParam('other').trim():'';
        var post_data = {'mobile': mobile, 'verify_code':verify_code, 'password':reg_pass, 'channel_id':channel_id, 'type':type, 'other':other, 'no_need_sign':true};
        $.post(CONFIG.API_URL+"/Home/UserCenter/signUp", post_data,
            function(response){
                var res_data = eval(response);
                if(res_data.code != "0"){
                    alert(res_data.msg);
                    $('#register').removeClass("maibei-disable-bgcolor").addClass("aui-btn");
		    sleep(3);
                    window.location.href = CONFIG.API_URL+'/weixinH5/templates/regin.html';
                }else{
                    toast.success({
                        title:"注册成功",
                        duration:2000
                    });
                    window.location.href = CONFIG.API_URL+'/weixinH5/templates/userdown.html';
                }
            }
        );
    });
});

//60秒倒计时
function verifyTimeout(val) {
    if (countdown == 0) {
        $("#get_verify").removeClass("maibei-aui-button").addClass("aui-btn");
        $("#get_verify").text("重获验证码");
        countdown = 60;
        return false;
    } else {
        $("#get_verify").removeClass("aui-btn").addClass("maibei-aui-button");
        $("#get_verify").text("(" + countdown + ")");
        countdown--;
    }
    setTimeout(function() {
        verifyTimeout(val)
    },1000);
}

function getVerifyCode(val) {
    //校验手机号是否正确
    var match_mobile = /^1[345678]\d{9}$/;
    var mobile = $("#phone").val().trim();
    if(!match_mobile.test(mobile)){
        toast.fail({
            title:"错误手机号",
            duration:2000
        });
        return false;
    }

    //清空原有的验证码
    $("#verify").val('');

    //发送验证码
    var post_data = {'mobile':mobile, 'type':VERIFY_CODE.TYPE_SIGNUP, 'no_need_sign': true}
    $.post(CONFIG.API_URL+"/Home/Customer/getVerifyCode", post_data,
        function(response){
            var res_data = eval(response);
            if(res_data.code == RET_CODE.CUSTOMER_MOBILE_EXIST){
                var dialog = new auiDialog({});
                dialog.alert({
                    title:"温馨提示",
                    msg:'亲，你注册过找钱花，请直接下载APP申请提现!',
                    buttons:['下载APP提现']
                },function(ret) {
                    if (ret.buttonIndex == 1) {
                        window.location.href = 'http://a.app.qq.com/o/simple.jsp?pkgname=com.maibai.user';
                    }
                })
            }else if(res_data.code != "0"){
                toast.fail({
                    title:"发送失败",
                    duration:2000
                });
            }else{
                //60秒倒计时
                verifyTimeout(val);
                toast.success({
                    title:"发送成功",
                    duration:2000
                });
            }
        }
    );
}
